﻿using Qins.PluginLoader;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using TraceLog;

namespace UniversalQin.WebScript
{
    [ClassInterface(ClassInterfaceType.AutoDual)]
    [ComVisible(true)]
    public class ScriptCallbackObject
    {
        public string ExecMethodVoid(string MethodName)
        {

            var reobj = PluginsLoader.Instance.Run(MethodName, "");
            return reobj?.ToString();
        }

        public string ExecMethodEx(string MethodName, string Paras)
        {
            var reobj = PluginsLoader.Instance.Run(MethodName, Paras);
            return reobj?.ToString();
            //MessageBox.Show("【网页调用C#】:" + arg);
        }

        /// <summary>
        /// js执行指定插件的代码并记录日志
        /// </summary>
        /// <param name="MethodName">要调用的插件名</param>
        /// <param name="Paras">参数</param>
        /// <param name="Caller">调用者</param>
        /// <param name="log">日志标志</param>
        /// <returns></returns>
        public string ExecMethodandLog(string MethodName,string Paras,string Caller, string log)
        {
            if (log == "true")
            {
                WriteLog(Caller, Paras);
            }
            var reobj = PluginsLoader.Instance.Run(MethodName, Paras);
            return reobj?.ToString();
        }

        public string WriteLog(params string[] log)
        {
            if(log.Length==1)
                Trace.Write(log[0]);
            if(log.Length==2)
                Trace.Write(new TraceLogType(WriteLogType.info, log[0]), log[1]);
            return "";
        }
    }
}
